package yartiss.engine.simulation.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import yartiss.engine.simulation.system.ITask;
import yartiss.engine.simulation.system.schedulable.SchedulableFactory;
import yartiss.engine.simulation.system.schedulable.SimpleTask;

/**
 * 
 * @author Manar Qamhieh
 * 
 */
public class TextReader {
	private final Scanner scanner;

	public TextReader(String source) throws FileNotFoundException {
		this(new File(source));
	}

	public TextReader(InputStream source) {
		scanner = new Scanner(source);
	}

	public TextReader(File source) throws FileNotFoundException {
		scanner = new Scanner(source);
	}

	public List<List<ITask>> readAllTaskSets() {
		List<List<ITask>> sets = new ArrayList<List<ITask>>();
		List<ITask> set = null;
		int nbTasksets = 0, nbTasks = 0;

		String currentLine = scanner.nextLine();
		String[] tokens = currentLine.split(" ");

		if (tokens.length == 4) {
			if (tokens[0].toLowerCase().equals("nbtasksets"))
				nbTasksets = Integer.parseInt(tokens[1]);
			else
				throw new IllegalArgumentException(
						"number of tasksets is expected at the beginning of the file...");
			if (tokens[2].toLowerCase().equals("nbtasks"))
				nbTasks = Integer.parseInt(tokens[3]);
			else
				throw new IllegalArgumentException(
						"number of tasks is expected at the beginning of the file...");

			for (int i = 0; i < nbTasksets; i++) {
				// System.out.println("TASKSET["+i+"]");
				set = readTaskset(nbTasks);
				if (set != null)
					sets.add(set);
			}
		}
		return sets;
	}

	public List<ITask> readTaskset(int nbTasks) {

		List<ITask> tasks = new ArrayList<ITask>();
		ITask task;
		@SuppressWarnings("unused")
		String infoLine = scanner.nextLine();

		String currentLine;
		String[] tokens;
		for (int j = 0; j < nbTasks; j++) {
			currentLine = scanner.nextLine();
			tokens = currentLine.split(" ");
			String type = tokens[2].toLowerCase();
			if("simple".equals(type))
				task = readSimpleTask(tokens);
			else if("graph".equals(type))
				task = readGraphTask(tokens);
			else
				throw new IllegalArgumentException("Illegal task type...");
			if (task != null)
				tasks.add(task);
		}
		return tasks;
	}

	private ITask readSimpleTask(String[] tokens) {
		SimpleTask task = null;
		long start = 0, wcet = 0, wcee = 0, period = 0, deadline = 0;
		int priority = 0;
		String type = null;

		for (int i = 0; i < tokens.length; i++) {
			String param = tokens[i].toLowerCase();
			if("type".equals(param))
				type = tokens[i + 2];
			else if("period".equals(param))
				period = Long.parseLong(tokens[i + 2]);
			else if("cost".equals(param) || "wcet".equals(param))
				wcet = Long.parseLong(tokens[i + 2]);
			else if("deadline".equals(param))
				deadline = Long.parseLong(tokens[i + 2]);
			else if("firstrelease".equals(param) || "start".equals(param))
				start = Long.parseLong(tokens[i + 2]);
			else if("wcee".equals(param))
				wcee = Long.parseLong(tokens[i + 2]);
			else if("priorirty".equals(param))
				priority = Integer.parseInt(tokens[i + 2]);


		}
		if (type.toLowerCase().equals("simple"))
			task = (SimpleTask) SchedulableFactory.newInstance(type, start,
					wcet, wcee, period, deadline, priority);
		return task;
	}

	private ITask readGraphTask(String[] tokens) {
		// TODO Auto-generated method stub
		return null;
	}
}
